package minformer;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Hashtable;
import java.util.TimerTask;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;


public class WeatherReader extends TimerTask
{
	private String cityNum;
	
	public WeatherReader(String cityNum)
	{
		this.cityNum = cityNum;
	}
	
	public void run()
	{
		try
		{
			readWeatherData();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private void readFromXml(InputStream is, Hashtable result) throws XmlPullParserException, IOException
	{
		Reader reader = new InputStreamReader(is);
		
		KXmlParser xmlParser = new KXmlParser();
		
		xmlParser.setInput(reader);
		
		xmlParser.nextTag();
		xmlParser.require(XmlPullParser.START_TAG, null, "MMWEATHER");
		
		
		xmlParser.nextTag();
		xmlParser.require(XmlPullParser.START_TAG, null, "REPORT");
		String type = xmlParser.getAttributeValue(null, "type");
		System.out.println("type="+type);
		
		xmlParser.nextTag();
		xmlParser.require(XmlPullParser.START_TAG, null, "TOWN");
		String index = xmlParser.getAttributeValue(null, "index");
		String name = xmlParser.getAttributeValue(null, "sname");
		System.out.println("index="+index+", name="+name);
		
		while(xmlParser.nextTag() == XmlPullParser.START_TAG) 
		{
			xmlParser.require(XmlPullParser.START_TAG, null, "FORECAST");
			
			int day = Integer.parseInt(xmlParser.getAttributeValue(null, "day"));

			System.out.println("day: "+ day);
			
			while(xmlParser.nextTag() == XmlPullParser.START_TAG)
			{
				xmlParser.nextTag();
				String tagName = xmlParser.getName();
				
				if ("TEMPERATURE".equalsIgnoreCase(tagName))
				{
				}
				
				System.out.println("name: "+tagName);
			}

			xmlParser.require(XmlPullParser.END_TAG, null, "FORECAST");
		}
		
		
		//
		
		
	}
	
	private void readWeatherData() throws Exception
	{
		InputStream is = null;
		HttpConnection httpConn = null;
		
		try
		{
			httpConn = (HttpConnection)Connector.open("http://informer.gismeteo.ru/xml/"+cityNum+"_1.xml", Connector.READ_WRITE);
			httpConn.setRequestMethod("GET");
			
			int respCode = httpConn.getResponseCode();
			if (respCode != HttpConnection.HTTP_OK) throw new Exception("Server error: "+respCode);
			
			is = httpConn.openInputStream();
			
			Hashtable result = new Hashtable();
			
			readFromXml(is, result);
		}
		finally
		{
			if (is != null) is.close();
			if (httpConn != null) httpConn.close();
		}
	
		
/*		Hashtable result = new Hashtable(); 
		
		String enc = "WINDOWS-1251";
		
		String gmString = "Gismeteo.Ru</a></td>";
		String pString = new String(new char[] {0xC4, 0xE0, 0xE2, 0xEB, 0xE5, 0xED, 0xE8, 0xE5});
		String dayString = new String(new char[] {0xC4, 0xED, 0xE5, 0xEC});
		String nightString = new String(new char[] {0xCD, 0xEE, 0xF7, 0xFC, 0xFE});
		String precipString = new String(new char[] {0xCE, 0xE1, 0xEB, 0xE0, 0xF7, 0xED, 0xEE, 0xF1, 0xF2, 0xFC});
		String windString = new String(new char[] {0xC2, 0xE5, 0xF2, 0xE5, 0xF0});
		String humString = new String(new char[] {0xC2, 0xEB, 0xE0, 0xE6, 0xED, 0xEE, 0xF1, 0xF2, 0xFC});
		
		result.put("city", findText(htmlText, "class=big><center>", null, 1, '<', enc));
		
		result.put("d1",  findText(htmlText, gmString,     "align=center>", 1, '<', null));
		result.put("d1t", findText(htmlText, dayString,    "align=center>", 1, '<', null));
		result.put("n1t", findText(htmlText, nightString,  "align=center>", 1, '<', null));
		result.put("d1o", findText(htmlText, precipString, "/000",      1, '.', enc));
		result.put("p1",  findText(htmlText, pString,      "align=center>", 1, '<', null));
		result.put("h1",  findText(htmlText, humString,    "align=center>", 1, '<', null));
		String w1 = findText(htmlText, windString,   "align=center>", 1, '/', enc);
		w1 = replaceStr(w1, "<br>", " ");
		w1 = (w1.indexOf("штиль") == -1) ? replaceStr(w1, "<", "м/с") : replaceStr(w1, "<", "");
		result.put("w1", w1);
		
		result.put("d2",  findText(htmlText, gmString,     "align=center>", 2, '<', null));
		result.put("d2t", findText(htmlText, dayString,    "align=center>", 2, '<', null));
		result.put("n2t", findText(htmlText, nightString,  "align=center>", 2, '<', null));
		result.put("d2o", findText(htmlText, precipString, "/000",      2, '.', enc));
		result.put("p2",  findText(htmlText, pString,      "align=center>", 2, '<', null));
		result.put("h2",  findText(htmlText, humString,    "align=center>", 2, '<', null));
		String w2 = findText(htmlText, windString,   "align=center>", 2, '/', enc);
		w2 = replaceStr(w2, "<br>", " ");
		w2 = (w2.indexOf("штиль") == -1) ? replaceStr(w2, "<", "м/с") : replaceStr(w2, "<", "");
		result.put("w2", w2);
		
		ScrWeather.loadPhenomenaImage((String) result.get("d1o"), (String) result.get("d2o"));
		ScrWeather.setResults(result);
		*/
	}
}
